package com.three.lab.page.common;


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.three.lab.MainActivity;
import com.three.lab.R;
import com.three.lab.base.AppManager;
import com.three.lab.base.BaseActivity;
import com.three.lab.util.MyCountDownTimer;
import com.three.lab.util.Typefaces;
import com.three.lab.widget.Titanic;
import com.three.lab.widget.TitanicTextView;

/**
 * 开屏页
 * created by lqd on 2024-06-05.
 */
public class SplashActivity extends BaseActivity {

    private final int TOTAL_TIME = 6 * 1000;
    private MyCountDownTimer countDownTimer;
    private TextView tvCountDown;
    private TitanicTextView titanicTextView;

    @Override
    protected void initParms(Bundle bundle) {
        setTransparent(true);
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        titanicTextView = $(R.id.my_text_view);
        tvCountDown = $(R.id.count_down);
        countDownTimer = new MyCountDownTimer(TOTAL_TIME, 1000, new MyCountDownTimer.OnCallBack() {
            @Override
            public void onTicking(long millisUntilFinished) {
                tvCountDown.setText(millisUntilFinished / 1000 + getResources().getString(R.string.skip));
            }

            @Override
            public void onFinish() {
                tvCountDown.setEnabled(true);
                resetTimer();
                boolean hasEnter = AppManager.hasEnter();
                finish();
                if (hasEnter) {
                    startActivity(MainActivity.class);
                } else {
                    startActivity(GuideActivity.class);
                }
            }
        });
    }

    @Override
    protected void setListener() {}

    @Override
    protected void widgetClick(View v) {}

    @Override
    protected void initData(Context mContext) {
        countDownTimer.start();
        titanicTextView.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));
        // start animation
        new Titanic().start(titanicTextView);

    }

    private void resetTimer() {
        if (countDownTimer != null)
            countDownTimer.cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        resetTimer();
    }
}